package com.kye.express.business.welfare_integral.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemIntegralCenterListHeaderBinding;
import com.kye.express.utils.StringUtils;
import com.kye.model.bean.integral.IntegralHeaderBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 12/01/2018
 * @updated : 12/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 *
 *     积分中心头部item
 */

public class IntegralHeaderItemViewBinder extends ItemViewBinder<IntegralHeaderBean, IntegralHeaderItemViewBinder.ViewHolder> {

    private OnIntegralHeaderListener onIntegralHeaderListener;

    public IntegralHeaderItemViewBinder(OnIntegralHeaderListener onIntegralHeaderListener) {
        this.onIntegralHeaderListener = onIntegralHeaderListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_integral_center_list_header, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull IntegralHeaderBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private IntegralHeaderBean item;
        private ItemIntegralCenterListHeaderBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.sign)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onIntegralHeaderListener != null) {
                                onIntegralHeaderListener.onSignClicked(item);
                            }
                        }
                    });
            RxView.clicks(binding.consume)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onIntegralHeaderListener != null) {
                                onIntegralHeaderListener.onConsumeClicked(item);
                            }
                        }
                    });
            RxView.clicks(binding.strategy)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onIntegralHeaderListener != null) {
                                onIntegralHeaderListener.onStrategyClicked(item);
                            }
                        }
                    });
        }

        public void bindData(IntegralHeaderBean item) {
            this.item = item;
            binding.setData(item);
            int type = item.getIntegralUserBean().getType();
            if (type == 1) {
                binding.sign.setEnabled(false);
            } else {
                binding.sign.setEnabled(!item.isIntegralUpdateSuccess); // 点击签到操作
            }
            Double jdIntegral = Double.valueOf(item.getIntegralUserBean().getJdIntegral());
//            if (jdIntegral >= 10000) {
//                binding.integralTitle.setText("可用积分（万）");
//            } else {
//                binding.integralTitle.setText("可用积分");
//            }
            binding.integral.setText(StringUtils.getIntegralFormat2(jdIntegral));
        }
    }

    public interface OnIntegralHeaderListener {
        void onSignClicked(IntegralHeaderBean item);
        void onConsumeClicked(IntegralHeaderBean item);
        void onStrategyClicked(IntegralHeaderBean item);
    }

}
